// pages/makeCard/makeCard.js
const app = getApp()
const HttpReq = require('@/utils/HttpReq.js')
Page({

    /**
     * 页面的初始数据
     */
    data: {
      IsEmpty: false,
      show:false,
      cardId: null,
      cardClientId: null,
      dealType: 0,
      retailerId:'',
      cardList:[
      ]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    submitMakeCardReq(e){
      var card = e.currentTarget.dataset.item
      this.setData({
        cardId: card.id
      })
      if(this.data.cardId == null && this.data.cardClientId == null){
        wx.showToast({
          title: '非法进入，无法提交',
          icon: 'error',
          duration: 2000
        })
        return
      }
      if(this.data.dealType != 2 && this.data.dealType != 3){
        wx.showToast({
          title: '非法进入，无法提交',
          icon: 'error',
          duration: 2000
        })
        return
      }
      wx.navigateTo({
        url: '/pageA/pages/makeCard2/makeCard2?cardId=' + this.data.cardId + '&dealType=' + this.data.dealType,
      })





    },

    Confirm(){
      this.setData({
        show: false
      })
      if(this.data.cardId == null && this.data.cardClientId == null){
        wx.showToast({
          title: '非法进入，无法提交',
          icon: 'error',
          duration: 2000
        })
        return
      }
      if(this.data.dealType != 2 && this.data.dealType != 3){
        wx.showToast({
          title: '非法进入，无法提交',
          icon: 'error',
          duration: 2000
        })
        return
      }
      var url = app.globalData.hostIp + '/weixin/addConsumerReq'
      var Data = {
        clientId: app.globalData.userInfo.id,
        phoneNumber: app.globalData.userInfo.phoneNumber,
        dealType: this.data.dealType,
        cardId: this.data.cardId,
        cardClientId: this.data.cardClientId,
        clientName: app.globalData.userInfo.name
      }
      HttpReq.httpsResquest(url, Data, 'POST').then(res=>{
        if(res.code == 1){
          wx.showToast({
            title: '办理请求已成功发送至商家，等待商家确认',
            icon: 'success',
            duration: 4000
          })
        }
        else if(res.code = 0){
          wx.showToast({
            title: res.msg,
            icon: 'error',
            duration: 4000
          })
        }else{
          wx.showToast({
            title: '请求失败',
            icon: 'error',
            duration: 2000
          })
        }
      })
    },

    onLoad(options) {
      if(options.retailerId != null){
        console.log('扫码进来了')
        this.setData({
          dealType: 2,
          retailerId: options.retailerId
        })
        var url = app.globalData.hostIp + '/weixin/queryCardByRetailId?retailerId=' + options.retailerId
        HttpReq.httpsResquest(url, null, 'GET').then(res=>{
            console.log(res);
          if(res.code == 1){
            this.setData({
              cardList: res.data
            })
          }else{
            wx.showToast({
              title: res.msg,
              icon: 'error',
              duration: 1500
            })
          }
        })
      }
      else if(options.cardClientId != null){
          //续卡  cardClientId老卡的id
        this.setData({
          dealType: 3,
          cardClientId: options.cardClientId
        })
        var url = app.globalData.hostIp + '/weixin/querySameKindCard?cardClientId=' + options.cardClientId
        HttpReq.httpsResquest(url, null, 'GET').then(res=>{
          if(res.code == 1){
            if(res.data.length == 0){
              this.setData({
                IsEmpty: true
              })
            }
            else{
              this.setData({
                cardList: res.data
              })
            }
          }else{
            wx.showToast({
              title: res.msg,
              icon: 'error',
              duration: 1500
            })
          }
        })
      }else{
        wx.showToast({
          title: '非法进入',
          icon: 'error',
          duration: 1500
        })
      }
    },
    Cancel(){
      this.setData({
        show: false
      })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})